var express = require('express');
var router = express.Router();
var userdao = require('../dao/userdao');
var utils = require('../common/utils');

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.render('users/user', {});
});

router.get('/add', function (req, res, next) {
  res.render('users/user-add', {});
});

router.get('/changepassword', function (req, res, next) {
  res.render('users/changepassword', {});
});

router.get('/permission', function (req, res, next) {
  res.render('users/permission', {});
});

router.get('/list', function (req, res, next) {
  var page = req.query.page || 1; //页号
  var limit = req.query.limit || 10; //页大小
  var key = req.query.key || ''; //查询条件
  var status=req.query.status||'0';//用户状态
  var condition = {
    page: parseInt(page),
    limit: parseInt(limit),
    key: key,
    status:status,
    userid:req.session.user.userid
  };
  userdao.getlist(condition).then(function (data) {
    return res.json({
      code: 0,
      msg: '',
      count: data[1][0].total,
      data: data[0]
    });
  }, function (error) {
    return res.json({
      code: 1,
      msg: error.message
    });
  });
});

router.post('/add', function (req, res, next) {
  var username = req.body.username;
  var truename = req.body.truename;
  var password = req.body.password;
  var status = req.body.status;

  var salt = utils.generalShortUrl(utils.generalUUID());
  var dbpassword = utils.md5(password + salt);

  var params = {
    userid: utils.generalUUID(),
    username: username,
    password: dbpassword,
    salt: salt,
    status: status,
    truename: truename,
    createuserid:req.session.user.userid
  };

  userdao.add(params).then(function (data) {
    console.log(data);

    if (data) {
      return res.json({
        code: 1
      });
    } else {
      return res.json({
        code: 0
      });
    }
  });
});

router.post('/statuschange', function (req, res, next) {
  var userid = req.body.userid;
  var status = req.body.status;
  var params = {
    userid: userid,
    status: status
  };

  userdao.statuschange(params).then(function (data) {
    console.log(data);

    if (data == 1) { //修改成功
      return res.json({
        code: 1
      });
    } else {
      return res.json({
        code: 0
      });
    }
  });
});

router.post('/resetpassword', function (req, res, next) {
  var userid = req.body.userid || req.session.user.userid;
  var password = req.body.password || utils.generalShortUrl(utils.generalUUID()); //随机明文密码
  var salt = utils.generalShortUrl(utils.generalUUID()); //随机salt
  var dbpassword = '';
  if (password.length == 6) { //6位表示明文
    dbpassword = utils.md5(utils.md5(password) + salt);
  } else {
    dbpassword = utils.md5(password + salt);
  }

  var params = {
    userid: userid,
    password: dbpassword,
    salt: salt
  };

  userdao.resetpassword(params).then(function (data) {
    console.log(data);

    if (data == 1) { //修改成功
      return res.json({
        code: 1,
        password: password
      });
    } else {
      return res.json({
        code: 0
      });
    }
  });
});


module.exports = router;